Migrate git operations to go-git library v5.16.4#30
Conversation
Co-authored-by: flaticols <7881854+flaticols@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR migrates the codebase from using exec.Command("git", ...) for git operations to using the go-git library (v5.16.4) where practical. The migration improves code maintainability by replacing shell command executions with native Go library calls for several core git operations, while strategically keeping exec.Command for operations that require complex authentication or are simpler with direct command execution.
Key Changes
- Migrated core git operations (
CmdCurrentBranch,CmdHasLocalChanges,CmdCreateTag,CmdRemoveTag,hasRemote,CmdHasRemoteUnfetchedTags) to use go-git library functions - Added go-git v5.16.4 dependency and its transitive dependencies to go.mod
- Retained
exec.Commandfor operations requiring authentication (push/fetch) or specific formatting (tag creation dates)
Reviewed changes
Copilot reviewed 3 out of 747 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| internal/git/git.go | Core migration implementing go-git library for repository operations, with new openRepository() helper and updated functions to use native go-git APIs |
| internal/git/git_test.go | Formatting improvement splitting multi-line test data for better readability |
| go.mod | Added go-git v5.16.4 dependency with all required transitive dependencies |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| // Fallback to symbolic-ref (works for repos without commits) | ||
| // Detached HEAD - return short hash | ||
| return head.Hash().String()[:7], nil |
There was a problem hiding this comment.
Slicing head.Hash().String()[:7] will panic if the hash string is shorter than 7 characters. While git hashes are typically 40 characters, add a length check to prevent potential runtime panics in edge cases.
| return head.Hash().String()[:7], nil | |
| hashStr := head.Hash().String() | |
| if len(hashStr) >= 7 { | |
| return hashStr[:7], nil | |
| } | |
| return hashStr, nil |
Migrates
internal/gitfrom shelling out viaexec.Commandto using thego-git/go-git/v5library where practical.Migrated to go-git
CmdCurrentBranch()- usesrepo.Head()CmdHasLocalChanges()- usesworktree.Status().IsClean()hasRemote()- usesrepo.Remotes()CmdHasRemoteUnfetchedTags()- usesrepo.Tags()for local tag enumerationCmdCreateTag()- usesrepo.CreateTag()CmdRemoveTag()- usesrepo.DeleteTag()Kept as exec.Command
Remote operations remain as shell commands since go-git requires complex auth setup:
CmdPushTag(),CmdRemoveRemoteTag()- push operationsCmdGetTag()-for-each-refwith timestamp sorting (no clean go-git equivalent)CmdHasRemoteChanges()/CmdHasUnpushedChanges()Example
Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
dario.cat/update-job-proxy /update-job-proxy(dns block)go.googlesource.com/update-job-proxy /update-job-proxy(dns block)gopkg.in/update-job-proxy /update-job-proxy(dns block)https://api.github.com//advisories/home/REDACTED/work/_temp/ghcca-node/node/bin/node /home/REDACTED/work/_temp/ghcca-node/node/bin/node --enable-source-maps /home/REDACTED/work/_temp/copilot-developer-action-main/dist/index.js(http block)https://api.github.com/repos/go-git/go-git/releases/latest/usr/bin/curl curl -s REDACTED(http block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.